package cx3k.scheduler.objects.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import cx3k.scheduler.objects.Course;
import cx3k.scheduler.objects.GroupedCourse;
import cx3k.scheduler.wrappers.CourseWrapper;

public class CourseUtil {

	
	/**
	 * Log information to specified location.
	 */
	private static Logger logger = Logger.getLogger(CourseWrapper.class);
	
	private static Logger getLogger(){
		return logger;
	}
	
	public static List<GroupedCourse> parsePreReqs(GroupedCourse course){
		return null;
	}
	
	private GroupedCourse makeCourse(String course){
		String degreeProgram = course.substring(0, 4);
		String courseNumStr = course.substring(4);
		int courseNum;
		
//		GroupedCourse groupedCourse course= .
		return null;
	}
	
	private List<GroupedCourse> loadCourses(String[] courseIds) {
		
		getLogger().trace("loadCourses(courseIds.length = " + courseIds.length + 
						  ") invoked: Loading courses from storage.");
		
		List<GroupedCourse> courses = new ArrayList<GroupedCourse>(courseIds.length);
		
		Course loaded;		// the loaded course from storage
		int courseId;		// the parsed course ID from the string
		
		// need to catch if data access service is not available
		try{
			
			// go through the array of course IDs
			for(int i = 0; i < courseIds.length; i++){
				
				// need to catch if the string is not a valid course ID (int)
				try{
					
					/*// parse the course ID to an integer
					courseId = Integer.parseInt(courseIds[i]);
					
					// load the Course from persistent storage
					loaded = StandardDataAccessService.getStandardDataAccessService().getCourse(courseId);
					
					// check if an object was loaded
					if(loaded != null){
					*/
						// object loaded, add to list
						courses.add(makeCourse(courseIds[i]));
					/*
						getLogger().trace("Added course \"" + 
										  loaded.getDegreeProgram() + ": " + 
										  loaded.getCourseNum() + 
										  "\" to the list of courses.");
					} else {
					
						// no object was loaded, indicates invalid ID
						getLogger().warn("The course ID " + courseIds[i] + 
										 " did not map to an object from the " +
										 "database, and could not be loaded.");
					}
					*/
				} catch(NumberFormatException e){
					
					// could not parse the course ID, move not next element
					getLogger().warn("The course ID " + courseIds[i] + 
									 " could not be parsed to an integer, and so " +
									 "the course could not be loaded from the database: " + 
									 e.getMessage());
				}
	 		}
		} catch (Exception e){
			// TODO 
		}
		
		/*catch(DataAccessException ex){
			
			// could not access persistent storage, end
			getLogger().warn("Problem loading the Data Access Service (\"" + 
							 ex.getMessage() + 
							 "\"); could not load the Course objects from storage.");	
		}*/
		
		getLogger().trace("loadCourses(String[] courseIds) returning a List of size " + 
					  	  courses.size() + ".");
		
		return courses;
	}
}
